package org.mriassl.service.user.web;

import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.mriassl.service.user.domain.User;
import org.mriassl.service.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private Registration client1;

	@Autowired
	private DiscoveryClient client2;

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/hello", method=RequestMethod.GET)
	public String index() throws InterruptedException{
		List<String> services = client2.getServices();
		logger.info("/hello, client1:serviceid:"+client1.getServiceId()+", client2:"+services.toString());
		
		int sleepTime = new Random().nextInt(3000);
		logger.info("sleepTime:"+sleepTime);
		
		Thread.sleep(sleepTime);
		
		return "Hello World";
	}

	@RequestMapping(value="/user", method=RequestMethod.GET)
	public User user(String id){
		System.out.println("id:"+id);
		return userService.getByKey(id);
	}

	@RequestMapping(value="/users", method=RequestMethod.GET)
	public List<User> user(String[] ids){
		System.out.println("ids:"+ids);
		return userService.findByIds(ids);
	}
}
